﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace D2DEngine
{
    public class D2DInputManager
    {

        public KeyboardState CurrentKeyboardState;
        public KeyboardState LastKeyboardState;

        public GamePadState CurrentGamePadState;
        public GamePadState LastGamePadState;

        public MouseState CurrentMouseState;
        public MouseState LastMouseState; 

        private static D2DInputManager instance = null;

        private D2DInputManager()
        {}
 
        
        /// <summary>
        /// Instance of the SceneManager
        /// </summary>
        public static D2DInputManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new D2DInputManager();

                return instance;
            }
        }      

        public void Update()
        {
            LastKeyboardState = CurrentKeyboardState;
            LastGamePadState = CurrentGamePadState;
#if(!XBOX)
            LastMouseState = CurrentMouseState;
#endif
            CurrentKeyboardState = Keyboard.GetState();
            CurrentGamePadState = GamePad.GetState(PlayerIndex.One);
#if(!XBOX)
            CurrentMouseState = Mouse.GetState();
#endif
        }



        /// <summary>
        /// Helper for checking if a key was newly pressed during this update.
        /// </summary>
        public bool MouseLeftButtonPressed
        {
            get 
            {
                return (CurrentMouseState.LeftButton == ButtonState.Pressed 
                    && LastMouseState.LeftButton == ButtonState.Released);
            }
        }

        public bool MouseRightButtonPressed
        {
            get
            {
                return (CurrentMouseState.RightButton == ButtonState.Pressed
                    && LastMouseState.RightButton == ButtonState.Released);
            }
        }

        /// <summary>
        /// Checks if a key has been pushed and released
        /// </summary>
        public bool IsKeyPressed(Keys key)
        {
            return (CurrentKeyboardState.IsKeyDown(key) &&
                    LastKeyboardState.IsKeyUp(key));
        }


        /// <summary>
        /// Checks if a key is pushed
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsKeyDown(Keys key)
        {
            return CurrentKeyboardState.IsKeyDown(key);
        }

        /// <summary>
        /// Checks if a key is released
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool IsKeyUp(Keys key)
        {
            return CurrentKeyboardState.IsKeyUp(key);
        }

        /// <summary>
        /// Helper for checking if a key was newly pressed during this update.
        /// </summary>
        public bool IsNewKeyPress(Keys key)
        {
            return (CurrentKeyboardState.IsKeyDown(key) &&
                    LastKeyboardState.IsKeyUp(key));
        }


        /// <summary>
        /// Helper for checking if a button was newly pressed during this update.
        /// </summary>
        public bool IsNewButtonPress(Buttons button)
        {
            return (CurrentGamePadState.IsButtonDown(button) &&
                    LastGamePadState.IsButtonUp(button));
        }
    }
}
